import qs from 'qs'
// 根路径
export const BASE_URL = 'https://a.dderp.cn/mob/'
//export const BASE_URL = 'http://192.168.1.35:10082/'
//export const BASE_URL = 'http://192.168.111.114:10082/'
//export const BASE_URL = 'http://192.168.31.28:10082/'
//export const BASE_URL = 'http://localhost:10082/'
export const PAY_URL = 'https://ad-kcc.dderp.cn/mob/'
//export const PAY_URL = 'http://localhost:10083/'
// 本地
 // const BASE_URL = 'http://localhost:10082/'
// 请求拦截
export const PostBody = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: params,
			method: 'POST',
			header: {
				"Token": token || '',
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}

export const PostBodyWithoutToken = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = "0038eb36d404bc9bd308435b52b2daea"
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: params,
			method: 'POST',
			header: {
				"Token": token || '',
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}

export const PostKccBody = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = "0038eb36d404bc9bd308435b52b2daea"
	return new Promise((resolve, reject) => {
		uni.request({
			url: PAY_URL + url,
			data: params,
			method: 'POST',
			header: {
				"KccToken": token || '',
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					//uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				//uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}


// 请求拦截
export const PostForm = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = uni.getStorageSync('token')
	/*#ifdef MP-WEIXIN*/
	const accountInfo = uni.getAccountInfoSync();
	const appid = accountInfo.miniProgram.appId;
	params.appid = appid;
	/*#endif*/
    //console.log("post Form 查询参数："+JSON.stringify(params))
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url + '?' + qs.stringify(params),
			data: {},
			method: 'POST',
			header: {
				"Token": token || uni.getStorageSync('token'),
				//"KccToken": token,
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					//uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				//uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}
export const PostFormWithNoToken = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = "0038eb36d404bc9bd308435b52b2daea";
	/*#ifdef MP-WEIXIN*/
	const accountInfo = uni.getAccountInfoSync();
	const appid = accountInfo.miniProgram.appId;
	params.appid = appid;
	/*#endif*/
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url + '?' + qs.stringify(params),
			data: {},
			method: 'POST',
			header: {
				"KccToken": token,
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					//uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				//uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}

export const PostKccForm = (url, params) => {
	uni.showLoading({
		title: '加载',
	})
	const token = "0038eb36d404bc9bd308435b52b2daea";
	return new Promise((resolve, reject) => {
		uni.request({
			url: PAY_URL + url + '?' + qs.stringify(params),
			data: {},
			method: 'POST',
			header: {
				"KccToken": token,
				"content-type": "application/json",
			},
			success: (res) => {
				status(res.statusCode, () => {
					//uni.hideLoading()
					resolve(res.data)
				})
			},
			fail: (res) => {
				reject(res)
				//uni.hideLoading()
				uni.showModal({
					title: '网络连接异常，请稍后再试',
					showCancel: false,
					success:function(res){
						if(res.confirm){
							uni.exitMiniProgram();
						}
					}
				})
			},
			complete: (res) => {
				uni.hideLoading();
			}
		})
	})
}


// 判断状态码
const status = (sts, callback) => {
	switch (sts) {
		case 200:
			callback()
			break;
		case 400:
			// callback()
			uni.showToast({
				title: '400错误的请求',
				icon: 'none'
			})
			break;
		case 401:
			// callback()
			/*uni.showToast({
				title: '401你的账号在别处登录，请重新登录！',
				icon: 'none'
			})*/
			uni.hideLoading()
			uni.showModal({
				title: '内部服务器401错误，请稍后再试',
				success(res) {
					uni.removeStorageSync('token')
					uni.removeStorageSync('list')
					uni.removeStorageSync('this')
					uni.removeStorageSync('session')
					/*uni.navigateTo({
						url: '/pages/login/login'
					})*/
					uni.exitMiniProgram();
				}
			})
			break;
		case 500:
			uni.hideLoading()
			uni.showModal({
				title: '内部服务器500错误，请稍后再试',
				success(res) {
					uni.removeStorageSync('token')
					uni.removeStorageSync('list')
					uni.removeStorageSync('this')
					uni.removeStorageSync('session')
					/*uni.navigateTo({
						url: '/pages/login/login'
					})*/
					uni.exitMiniProgram();


				}
			})
			break;
	}
}


// 微信支付类型的接口
export const pay = (url, data) => {
	let token = uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: PAY_URL + url  + '?' + qs.stringify(data),
			method: 'POST',
			header: {
				"Token": token || '',
				"content-type": "application/json",
			},
			success: (res) => {
				uni.hideLoading()
				resolve(res.data)
			},
			fail: (err) => {
				uni.hideLoading()
				uni.showToast({
					title: '网络错误，请稍后再试',
					icon: 'none'
				})
				reject(err)
			}
		})
	 })
}